package com.wang.common.views;

import com.wang.mandkey.tool.Tool;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.RelativeLayout;

public class SlidableButton extends Button{
	
  private boolean isDragging = false;
  private boolean isAnimating = false;
  private String tag="SlidableButton";
  private Context mContext;
  
  public SlidableButton(Context context) {
      super(context);
      this.mContext=context;
  }
  
  public SlidableButton(Context context, AttributeSet attrs){
      super(context, attrs);
      this.mContext=context;
  }
  
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
      isDragging = false;
      return true;
    }
     else if(event.getAction()==MotionEvent.ACTION_MOVE){
    	  isDragging=true;
    	  int rx=(int)event.getRawX();
    	  if(rx<=10){
    		  rx=10;
    	  }else{
    		  int vx = Tool.getDisplayMetrics(mContext).x;
    		  if(rx>(vx-30)){
    			  rx=vx-30;
    		  }
    	  }
    	 setX(rx);
    }
    return false;
  }
  

  public boolean isAnimating() {
    return isAnimating;
  }
  
  public void setDragging(boolean isDragging) {
    this.isDragging = isDragging;
  }
  public boolean isDragging() {
    return isDragging;
  }
  public void setX(int x){
    if(isDragging && !isAnimating){
      Log.i(tag, "x is:"+x);
      RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams)getLayoutParams();
      lp.leftMargin = x;
      setLayoutParams(lp);
    }
  }
  
}